﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProtoBuf;

namespace ZyGames.DZPocker.Model
{
	[Serializable, ProtoContract]
	public class InsuranceModel
	{
		public InsuranceModel()
		{
			ListUsr = new List<UserAndOuts>();
			Common = new List<int>();
			Outs = new List<int>();
		}

		[ProtoMember(1)]
		public int TableId { get; set; }

		/// <summary>
		/// 保险操作时间(毫秒)
		/// </summary>
		[ProtoMember(2)]
		public int Time { get; set; }

		/// <summary>
		/// 底池筹码
		/// </summary>
		[ProtoMember(3)]
		public int AntNum { get; set; }

		/// <summary>
		/// 赔率 1:odds
		/// </summary>
		[ProtoMember(4)]
		public float Odds { get; set; }

		/// <summary>
		/// 所有玩家手牌
		/// </summary>
		[ProtoMember(5)]
		public List<UserAndOuts> ListUsr { get; set; }

		/// <summary>
		/// 桌子上的公共牌
		/// </summary>
		[ProtoMember(6)]
		public List<int> Common { get; set; }

		/// <summary>
		/// outs牌
		/// </summary>
		[ProtoMember(7)]
		public List<int> Outs { get; set; }

		/// <summary>
		/// 当前局下注的数量(投入)
		/// </summary>
		[ProtoMember(8)]
		public int Chip { get; set; }

		/// <summary>
		/// 可购买的最大筹码
		/// </summary>
		[ProtoMember(9)]
		public int MaxBuyChip { get; set; }

		/// <summary>
		/// 购买保险的数量
		/// </summary>
		//public int InsureChip { get; set; }

		/// <summary>
		/// 赔付额
		/// </summary>
		public int Compensate { get; set; }

		/// <summary>
		/// 是否购买保险
		/// </summary>
		public bool BuyInsure { get; set; }

		/// <summary>
		/// 每一回合临时存放的outs变量
		/// </summary>
		private Dictionary<int, List<int>> tempOuts = new Dictionary<int, List<int>>();
		private Dictionary<int, int> tempCompensates = new Dictionary<int, int>();

		//所有人的outs牌
		private Dictionary<int, List<List<int>>> allOuts = new Dictionary<int, List<List<int>>>();
		private Dictionary<int, List<float>> allOdds = new Dictionary<int, List<float>>();
		private Dictionary<int, List<int>> allInsuranceChips = new Dictionary<int, List<int>>();
		private Dictionary<int, List<int>> allCompensates = new Dictionary<int, List<int>>();


		public void RoundClear()
		{
			this.tempOuts.Clear();
			this.tempCompensates.Clear();
		}

		public bool AddTempCompensates(int posId, int compensate)
		{
			int tempCom;
			if (tempCompensates.TryGetValue(posId, out tempCom))
			{
				return false;
			}
			else
				tempCompensates.Add(posId, tempCom);
			return true;
		}

		public int GetCompensateByPosId(int posId)
		{
			int tempCom;
			if (tempCompensates.TryGetValue(posId, out tempCom))
			{
				return tempCom;
			}
			else
				return 0;
		}

		public Dictionary<int, List<int>> TempOuts()
		{
			return this.tempOuts;
		}

		public List<int> GetTempOutsByPosId(int posId)
		{
			List<int> tOuts;
			if (tempOuts.TryGetValue(posId, out tOuts))
			{
				if (tOuts.Count > 0)
					return tOuts;
			}
			return null;
		}

		public bool AddTempOuts(int posId, List<int> outs)
		{
			List<int> tOuts;
			if (!tempOuts.TryGetValue(posId, out tOuts))
			{
				tempOuts.Add(posId, outs);
				return true;
			}
			else
				return false;
		}

		public bool AddOuts(int posId, List<int> outs)
		{
			List<List<int>> tempOuts;
			if (allOuts.TryGetValue(posId, out tempOuts))
				tempOuts.Add(outs);
			else
			{
				tempOuts = new List<List<int>>();
				tempOuts.Add(outs);
			}
			allOuts.Add(posId, tempOuts);
			return true;
		}

		public List<int> GetOuts(int posId, int index)
		{
			List<List<int>> tempOuts;
			if (allOuts.TryGetValue(posId, out tempOuts))
			{
				var outs = tempOuts[index];
				if (outs.Count > 0)
					return outs;
			}
			return null;
		}

		public bool AddOdds(int posId, float odds)
		{
			List<float> tempOdds;
			if (allOdds.TryGetValue(posId, out tempOdds))
				tempOdds.Add(odds);
			else
			{
				tempOdds = new List<float>();
				tempOdds.Add(odds);
			}
			allOdds.Add(posId, tempOdds);
			return true;
		}

		public bool AddInsuranceChips(int posId, int chipNum)
		{
			List<int> tempChipNums;
			if (this.allInsuranceChips.TryGetValue(posId, out tempChipNums))
				tempChipNums.Add(chipNum);
			else
			{
				tempChipNums = new List<int>();
				tempChipNums.Add(chipNum);
				allInsuranceChips.Add(posId, tempChipNums);
			}
			return true;
		}

		public bool AddCompensate(int posId, int compensate)
		{
			List<int> tempCompensate;
			if (this.allCompensates.TryGetValue(posId, out tempCompensate))
				tempCompensate.Add(compensate);
			else
			{
				tempCompensate = new List<int>();
				tempCompensate.Add(compensate);
				this.allCompensates.Add(posId, tempCompensate);
			}
			return true;
		}

		/// <summary>
		/// 最大可购买的筹码数量
		/// </summary>
		/// <param name="total"></param>
		/// <returns></returns>
		public int MaxCanBuyChipNum(int total, int posId)
		{
			List<int> chips;
			if (this.allInsuranceChips.TryGetValue(posId, out chips))
			{
				foreach (var chip in chips)
					total -= chip;
			}
			else //之前没买过则可以购买
				return total;
			return total <= 0 ? 0 : total;
		}


		public void Clear()
		{
			this.TableId = 0;
			this.AntNum = 0;
			this.Time = 0;
			this.ListUsr.Clear();
			this.Common.Clear();
			this.Outs.Clear();
			this.Chip = 0;
			//this.InsureChip = 0;
			this.Compensate = 0;
			this.BuyInsure = false;
			this.allOuts.Clear();
			this.allOdds.Clear();
			this.allInsuranceChips.Clear();
		}
	}
}
